package com.game.view.display
{
    import com.game.view.IAnimation;
    
    import flash.display.Bitmap;
    
    /**
     * 可以进行播放渲染的Bitmap影片剪辑
     */
    public class BitmapMovieClip extends AnimationClip implements IAnimation
    {
        private var _tsGroup:TextureGroup;
        
        private var _bp:Bitmap;
        
        public function BitmapMovieClip(tex:TextureGroup = null)
        {
            super();
            setTexture(tex);
        }
        
        public function setTexture(texGroup:TextureGroup):void
        {
            if(!texGroup) return;
            _tsGroup = texGroup;
            init();
        }
        
        public function getTexture():TextureGroup
        {
            return _tsGroup;
        }
        
        private function init():void
        {
            _currentFrame = 1;
            _totalFrames = _tsGroup.length;
            _bp = new Bitmap(null, "auto", true);
            addChild(_bp);
        }
        
        override protected function render():void
        {
            var tex:Texture = _tsGroup.getTextureAt(_currentFrame - 1);
            if (!(_bp.x == tex.pivotX && _bp.y == tex.pivotY))
            {
                _bp.x = tex.pivotX;
                _bp.y = tex.pivotY;
            }
            _bp.bitmapData = tex.content;
        }
        
        override public function Destroy():void
        {
            if(!_tsGroup) return;
            _tsGroup.destroy();
            _tsGroup = null;
        }
        
        public function clone():BitmapMovieClip
        {
            return new BitmapMovieClip(_tsGroup);
        }
    }
}
